/**
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.components.controls
{
	import com.actionpals.base.BaseSprite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
		
	public class Slider extends BaseSprite
	{
		/**
		 * @private
		 */
		protected var _track:DisplayObject;
		
		/**
		 * @private
		 */
		protected var _handle:DisplayObject;
		
		/**
		 * @private
		 */
		protected var _trackContainer:Sprite;
		
		/**
		 * @private
		 */
		protected var _handleContainer:Sprite;
		
		/**
		 * @private
		 */
		protected var _handleBounds:Rectangle;
		
		/**
		 * @private
		 */
		protected var _handleRect:Rectangle;
		
		/**
		 * @private
		 */
		protected var _target:DisplayObject;
		
		public function Slider()
		{			
			super()
			init();
		}
		
		/**
		 * The graphical representation of the slider's track
		 */
		public function get track():DisplayObject
		{
			return _track;
		}

		/**
		 * @private
		 */
		public function set track( value:DisplayObject ):void
		{
			try{ _trackContainer.removeChild( _track ) }catch( error:Error ){};
			
			_track = value;
			_trackContainer.addChild( _track );
		}
		
		/**
		 * The graphical representation of the slider's handle
		 */
		public function get handle():DisplayObject
		{
			return _handle
		}

		/**
		 * @private
		 */
		public function set handle( value:DisplayObject ):void
		{
			try{ _trackContainer.removeChild( _track ) }catch( error:Error ){};
			_handle = value;
		}
		
		/**
		 * Moves the _handle to a specified x and y position and calls dispatchUpdate.
		 * 
		 * @param xPos	The new x position.
		 * @param yPos	The new y position.
		 */
		public function updateHandle( xPos:Number = undefined, yPos:Number = undefined ):void
		{	
			var newX:Number = ( xPos-xPos == 0 ) ? xPos : _handle.x;
			var newY:Number = ( yPos-yPos == 0 ) ? yPos : _handle.y;
			var bounds:Rectangle = _track.getRect( this );
			var temp:Rectangle = new Rectangle( newX, newY, _handle.width, _handle.height );
			
			if( temp.right > bounds.right ) temp.x = bounds.right - temp.width;
			if( temp.left < bounds.left ) temp.x = bounds.x;
			if( temp.bottom > bounds.bottom ) temp.y = bounds.bottom - temp.height;
			if( temp.top < bounds.top ) temp.y = bounds.y;
			
			_handle.x = temp.x;
			_handle.y = temp.y;
			
			dispatchUpdate();
		}
		
		/**
		 * @private
		 */
		protected function init():void
		{
			
		}
		
		/**
		 * @private
		 */
		protected function dispatchUpdate():void
		{
			
		}
		
		
	}
}